import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

/// 内容编辑器
class ContentEditor extends StatefulWidget {
  final String content;
  final ValueChanged<String> onContentChanged;
  final String hintText;
  final int maxLength;
  final bool showWordCount;

  const ContentEditor({
    super.key,
    required this.content,
    required this.onContentChanged,
    this.hintText = '分享你的想法...',
    this.maxLength = 2000,
    this.showWordCount = true,
  });

  @override
  State<ContentEditor> createState() => _ContentEditorState();
}

class _ContentEditorState extends State<ContentEditor> {
  late TextEditingController _controller;
  late FocusNode _focusNode;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController(text: widget.content);
    _focusNode = FocusNode();
  }

  @override
  void dispose() {
    _controller.dispose();
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 10,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              const Text(
                '内容描述',
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w600,
                  color: Color(0xFF333333),
                ),
              ),
              const Spacer(),
              if (widget.showWordCount)
                Text(
                  '${_controller.text.length}/${widget.maxLength}',
                  style: TextStyle(
                    fontSize: 12,
                    color: _controller.text.length > widget.maxLength * 0.9
                        ? Colors.red
                        : const Color(0xFF999999),
                  ),
                ),
            ],
          ),
          const SizedBox(height: 12),
          TextField(
            controller: _controller,
            focusNode: _focusNode,
            maxLines: 8,
            maxLength: widget.maxLength,
            decoration: InputDecoration(
              hintText: widget.hintText,
              hintStyle: const TextStyle(
                color: Color(0xFF999999),
                fontSize: 16,
              ),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
                borderSide: const BorderSide(
                  color: Color(0xFFE0E0E0),
                  width: 1,
                ),
              ),
              enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
                borderSide: const BorderSide(
                  color: Color(0xFFE0E0E0),
                  width: 1,
                ),
              ),
              focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
                borderSide: const BorderSide(
                  color: Color(0xFF007AFF),
                  width: 1,
                ),
              ),
              contentPadding: const EdgeInsets.all(12),
              counterText: '', // 隐藏默认计数器
            ),
            style: const TextStyle(
              fontSize: 16,
              color: Color(0xFF333333),
              height: 1.5,
            ),
            onChanged: (value) {
              widget.onContentChanged(value);
              setState(() {}); // 更新字数统计
            },
            inputFormatters: [
              LengthLimitingTextInputFormatter(widget.maxLength),
            ],
          ),
          const SizedBox(height: 8),
          _buildToolbar(),
        ],
      ),
    );
  }

  Widget _buildToolbar() {
    return Row(
      children: [
        _buildToolButton(
          icon: Icons.emoji_emotions_outlined,
          onTap: () => _showEmojiPicker(),
        ),
        const SizedBox(width: 8),
        _buildToolButton(
          icon: Icons.location_on_outlined,
          onTap: () => _addLocation(),
        ),
        const SizedBox(width: 8),
        _buildToolButton(
          icon: Icons.tag_outlined,
          onTap: () => _addTag(),
        ),
        const Spacer(),
        _buildToolButton(
          icon: Icons.clear_all_outlined,
          onTap: () => _clearContent(),
        ),
      ],
    );
  }

  Widget _buildToolButton({
    required IconData icon,
    required VoidCallback onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.all(8),
        decoration: BoxDecoration(
          color: const Color(0xFFF8F9FA),
          borderRadius: BorderRadius.circular(6),
        ),
        child: Icon(
          icon,
          size: 20,
          color: const Color(0xFF666666),
        ),
      ),
    );
  }

  void _showEmojiPicker() {
    // TODO: 实现表情选择器
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(content: Text('表情选择功能开发中...')),
    );
  }

  void _addLocation() {
    // TODO: 实现位置添加功能
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(content: Text('位置添加功能开发中...')),
    );
  }

  void _addTag() {
    // TODO: 实现标签添加功能
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(content: Text('标签添加功能开发中...')),
    );
  }

  void _clearContent() {
    _controller.clear();
    widget.onContentChanged('');
    setState(() {});
  }
}

